﻿using RobosimAPI.Class;
using System.Data.SQLite;
using System.Text.Json;

namespace RobosimAPI.Command
{
	public static partial class Command
	{
		public static void GetThemeRuleList()
		{
			Theme[] themes = Theme.GetAllThemes();
			foreach (Theme item in themes)
			{
				string httpResult = Tools.SignHttpPost("https://robosim.zmrobo.com/back/Api2/Client/themeRuleList", "user_id=6&region_id=0&theme_id="+item.Id, Tools.GetTimestamp().ToString(), "309ede363e761a8c78400ca570a94c4f", "6");
				ThemeRuleList theme = JsonSerializer.Deserialize<ThemeRuleList>(httpResult);
				using (var connection = new SQLiteConnection(Account.DB_CONNECTION_STRING))
				{
					connection.Open();
					using (var transaction = connection.BeginTransaction())
					{
						using (var command = new SQLiteCommand(connection))
						{
							for (int i = 0; i < theme.rule.Count; i++)
							{
								command.CommandText = $"INSERT INTO themeRuleList (id,title,version,theme_id) VALUES (@id, @title, @version,@theme_id)";
								command.Parameters.AddWithValue("@id", theme.rule[i].id);
								command.Parameters.AddWithValue("@title", theme.rule[i].title);
								command.Parameters.AddWithValue("@version", theme.rule[i].version);
								command.Parameters.AddWithValue("@theme_id", item.Id);
								command.ExecuteNonQuery();
							}
						}
						transaction.Commit(); // 提交事务
					}
				}
			}
		}
		public class RuleItem
		{
			public int id { get; set; }
			public string title { get; set; }
			public string version { get; set; }
		}

		public class ThemeRuleList
		{
			public int status { get; set; }
			public List<RuleItem> rule { get; set; }
		}

	}
}
